{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>

<form class="layui-form" action="" id="cross_srh">
    <div class="proxy_srh">
        <div class="layui-inline">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label class="layui-form-label">project_name：</label>
                    <div class="layui-input-inline">
                        <select class="layui-select" name="project_name" id="project_name" lay-verify="" lay-search
                                lay-filter="project_name">

                            {% for d in projectAll %}
                                <option value="{{d.id}}">{{d.project_name}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="layui-input-inline">
                    <label class="layui-form-label">module_name：</label>
                    <div class="layui-input-inline" id="module_div">
                       <select class="layui-select" name="module_name" id="module_name" lay-verify="" lay-search
                                lay-filter="module_name">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>

<br><br>
                <div class="layui-input-inline">
                    <label class="layui-form-label">path：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="path" id="path" placeholder="请输入" style="width:212px">
                    </div>
                </div>

                <div class="layui-input-inline">
                    <label class="layui-form-label">effect：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="effect" id="effect" placeholder="请输入"
                               style="width:212px">
                    </div>
                </div>

                <div class="layui-inline" style="left: 257px">
                    <button class="layui-btn" type="button" id="start" lay-submit lay-filter="start">查询</button>
                </div>
            </div>
        </div>

    </div>
</form>
<hr class="layui-bg-gray">
<button class="layui-btn  layui-btn-normal" id="add" type="button">
    <i class="layui-icon"></i> 创建案例
</button>
<button class="layui-btn   " id="crtPlan" type="button">
    <i class="layui-icon"></i> 创建计划
</button>

<button class="layui-btn  layui-btn-normal layui-btn-danger" id="runAll" type="button">
    <i class="layui-icon"></i> 立即执行
</button>



<script type="text/html" id="filemange_v1">
    <button type="button"  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="update">更新</button>
    <button type="button"  class="layui-btn layui-btn-xs " lay-event="run">运行</button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delNode">
    <i class="layui-icon">&#xe640;</i>
  </button>
</script>

<table class="layui-hide" id="apiCase" lay-filter="apiCase"></table>

<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var loading = layer.load();

          var  options = {
                elem:'#apiCase',
                skin:"line",
                page:true,
                height: 'full-200',
                toolbar: true,
                url:'/case/api/srh?',
                limit:10,
                limits:[10,50,100],
                where:{
                    project_id : $('#project_name option:selected').val(),
                    module_id: $('#module_name option:selected').val(),
                    effect: $('#effect').val(),
                    path: $('#path').val()
                },
                response:{
                    statusName:'errorCode',
                    statusCode:'',
                    dataName:'data'
                },
                cols:[[
                       {type: 'checkbox'}
                      ,{type: 'numbers',title:'No.'}
                      ,{field: 'case_id', title: 'caseId',sort:true}
                      ,{field: 'effect', title: 'effect',sort:true}
                      ,{field: 'host', title: 'host',sort:true}
                      ,{field: 'path', title: 'path',sort:true}
                      ,{field: 'method', title: 'fun',sort:true}
                      ,{field: 'headersJson', title: 'header',sort:true,width:1,templet: function(d){
                            jStr =  JSON.stringify(d.headersJson, null, 4);
                            return jStr;
                      }}
                      ,{field: 'bodysJson', width:1,title: 'bodys',sort:true,templet: function(d){
                            jStr =  JSON.stringify(d.bodysJson, null, 4);
                            return jStr;
                      }}
                      ,{field: 'pre_caseId', title: 'pre_Id',sort:true,width:100}
                      ,{field: 'chkJson', width:1,title: 'chk',sort:true,templet: function(d){
                            jStr =  JSON.stringify(d.chkJson, null, 4);
                            return jStr;
                      }}
                      ,{field: 'concurrent', title: 'con.',sort:true}
                      ,{field: 'run_times', title: 'times',sort:true}
                      ,{field: 'person', title: 'person',sort:true,hide:true,templet: function(d){
                         return d.person_name;
                      }}
                      ,{field: 'note',hide:true, title: 'note',sort:true}
                      ,{field: 'last_time', title: 'last',sort:true,hide:true}
                      ,{field: 'req_type', title: 'req_type',sort:true,hide:true}
                      ,{field: 'module_id', title: 'module_id',sort:true,hide:true}
                      ,{field: 'project_id', title: 'project_id',sort:true,hide:true}
                      ,{field: 'id', title: 'id',sort:true,hide:true}
                      ,{fixed: 'right', title:'', toolbar: '#filemange_v1',width:170}

                ]],
                id:'apiCase',
                done:function(res,cur,count){

                    //$("[data-field='req_type']").css('display','none');
                    //$("[data-field='module_id']").css('display','none');
                    //$("[data-field='project_id']").css('display','none');
                    $("[data-field='id']").css('display','none');
                    $("[data-field='headersJson']").css('display','none');

                    $("[data-field='bodysJson']").css('display','none');
                    $("[data-field='chkJson']").css('display','none');

                    layer.close(loading);
                }
              };
          var tableIns = table.render(options);

          table.on('row(apiCase)', function(obj){
                obj.tr.css("background","#eeeeee");
          });
          table.on('rowDouble(apiCase)', function(obj){
            msg = JSON.stringify(obj.data, null, 4);

            layer.open({
              title: obj.data.case_id + '_案例',
              area:['1055px','473px']
              ,content: '<pre>'+msg+'</pre>'
              ,closeBtn:1,
              shade: 0,
              shadeClose:true
            });

          });
          form.on('select(project_name)', function(obj){
                $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/moudle/all/select?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:{"project_id":obj.value,"moudle_type":"1"},
                        success: function(result){
                            s1 = '<div class="layui-input-inline"> <select class="layui-select" name="module_name" id="module_name" lay-verify="" lay-search lay-filter="module_name"><option value="">请选择</option>';
                            e1 = '</select></div>';
                             if(result.data.length>0){

                                str = '';
                                layui.each(result.data,function(k,v){
                                        str = str+'<option value="'+v.id+'">'+v.moudle_name+'</option>'
                                });
                                htmlDiv = s1+str+e1;
                                $("#module_div").html(htmlDiv);


                             }else{
                                $("#module_div").html(s1+e1);
                             }
                             form.render();


                       }
                    });
                  form.render('select');
          });

          //crtPlan
          $('#crtPlan').on('click',function(){
              var res = [];
              var checkStatus = table.checkStatus('apiCase').data;
              layui.each(checkStatus,function(k,v){
                    res.push(v.id);
              });
              if(res.length==0){layer.msg("没有选中行");return false;}
              layui.data('test', {key: 'apiPlanCache',value: {
                    id:"",
                    project_id:1,
                    module_id:1,
                    api_plan_name:"",
                    auto_order:"",
                    caseIdList:res,
              }});
              index=layer.open({
                                                type:2,
                                                content:"/case/plan/manage?token={{token}}&openId={{openId}}",
                                                area:['425px','527px'],
                                                //offset: ['231px','307px'],
                                                title:"任务管理",
                                                closeBtn:1,
                                                shade: 0,
                                                shadeClose:true,
                                                //maxmin: true,
                                                cancel: function(index, layero){

                                                }
                });
          });
          $('#runAll').on('click',function(){
              var res = [];
              var checkStatus = table.checkStatus('apiCase').data;
              layui.each(checkStatus,function(k,v){
                    $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/case/api/run/byId?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        async: false,
                        data:{"caseId":v.id},
                        success: function(result){
                             res.push(result);
                       }
                    });
                    //res.push(v.id);
              });
              if(res.length>0){
                  if($(".layui-layer-close").length == 0 ){
                            divRunContent = '<div class="layui-inline"><pre id="json"></pre></div>';
                            index = layer.open({
                                        type:1,
                                        skin: 'demo-class',
                                        content:divRunContent,
                                        area:['666px','346px'],
                                        offset:  'lb',
                                        title:"批量运行结果",
                                        closeBtn:1,
                                        shade: 0,
                                        id:"runApiCase",
                                        shadeClose:true,
                                        maxmin: true,
                                        success: function(index, layero){

                                        }
                            });
                  }
                  $('#json').jsonViewer(res);
              }
          });

          //点击添加
          $('#add').on('click',function(){
            layui.data('test', {key: 'apiCase',value: undefined});
            index=layer.open({
                                        type:2,
                                        content:"/api/case/manage?token={{token}}&openId={{openId}}",
                                        area:['1055px','473px'],
                                        //offset: ['231px','307px'],
                                        title:"创建案例",
                                        closeBtn:1,
                                        shade: 0,
                                        shadeClose:true,
                                        maxmin: true,
                                        cancel: function(index, layero){
                                             $('#start').click();
                                        }
            });
            layer.full(index);

          });

          //更新或者删除案例
          table.on('tool(apiCase)', function(obj){
                if (obj.event ==='run'){

                    if($(".layui-layer-close").length == 0 ){
                        divRunContent = '<div class="layui-inline"><pre id="json"></pre></div>';
                        index = layer.open({
                                    type:1,
                                    skin: 'demo-class',
                                    content:divRunContent,
                                    area:['666px','346px'],
                                    offset:  'lb',
                                    title:obj.data.path + "——运行",
                                    closeBtn:1,
                                    shade: 0,
                                    id:"runApiCase",
                                    shadeClose:true,
                                    maxmin: true,
                                    success: function(index, layero){

                                    }
                        });
                    }
                    $.ajax({
                         //发出请求
                        type:"GET",
                        url:"/case/api/run/byId?",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:{"caseId":obj.data.id},
                        success: function(result){
                             $('#json').jsonViewer(result);
                       }
                    });
                    return false;

                } else if(obj.event === 'update'){
                    layui.data('test', {key: 'apiCase',value: obj.data});
                    index=layer.open({
                                                type:2,
                                                content:"/api/case/manage?token={{token}}&openId={{openId}}",
                                                area:['1055px','473px'],
                                                //offset: ['231px','307px'],
                                                title:"维护案例",
                                                closeBtn:1,
                                                shade: 0,
                                                shadeClose:true,
                                                maxmin: true,
                                                cancel: function(index, layero){
                                                     $('#start').click();
                                                }
                    });
                    layer.full(index);

                }else if(obj.event==='delNode'){
                    layer.confirm('真的删除行么?', function(index){
                        $.ajax({
                                    //发出请求
                                    type:"POST",
                                    url:"/case/api/del",
                                    contentType: 'application/json; charset=utf-8',
                                    dataType:"json",
                                    data:JSON.stringify({"id":obj.data.id}),
                                    success: function(result){
                                        obj.del();
                                        layer.close(index);
                                        layer.msg("删除成功!");
                                    }
                        });
                      });

                }
          });

          //点击查询
          $('#start').on('click',function(){
              var loading = layer.load();

              options = {
                elem:'#apiCase',
                skin:"line",
                page:true,
                height: 'full-200',
                toolbar: true,
                url:'/case/api/srh?',
                limit:10,
                limits:[10,50,100],
                where:{
                    project_id : $('#project_name option:selected').val(),
                    module_id: $('#module_name option:selected').val(),
                    effect: $('#effect').val(),
                    path: $('#path').val()
                }
              }
              tableIns.reload(options);
              layer.close(loading);
          });




          form.render();
          layer.close(loading);

    });
</script>
{% endblock %}